package ckw.database.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import core.database.AbstractEntity;

@Entity
@Table(name = "electionResidentVoter")
public class ElectionResidentVoter extends AbstractEntity {
	@Column(name = "hasVoted", nullable = false)
	private Boolean hasVoted = false;

	@ManyToOne(cascade = { CascadeType.DETACH })
	@JoinColumn(name = "voter", insertable = true, updatable = true)
	private Voter voter;

	@ManyToOne
	@JoinColumn(name = "electionNode", insertable = true, updatable = true)
	private ElectionNode electionNode;

	public ElectionResidentVoter() {
		super();
		this.hasVoted = false;
	}

	public ElectionResidentVoter(Voter voter, ElectionNode electionNode) {
		super();
		this.hasVoted = false;
		this.voter = voter;
		voter.getElections().add(this);
		this.electionNode = electionNode;
		electionNode.getVoters().add(this);
	}

	public Boolean getHasVoted() {
		return hasVoted;
	}

	public void setHasVoted(Boolean hasVoted) {
		this.hasVoted = hasVoted;
	}

	public Voter getVoter() {
		return voter;
	}

	public void setVoter(Voter voter) {
		this.voter = voter;
	}

	public ElectionNode getElectionNode() {
		return electionNode;
	}

	public void setElectionNode(ElectionNode electionNode) {
		this.electionNode = electionNode;
	}

	public void removeFromElections() {
		voter.getElections().remove(this);
		voter = null;
		electionNode.getVoters().remove(this);
		electionNode = null;
	}

	public void addToElections(Voter voter, ElectionNode electionNode) {
		this.voter = voter;
		voter.getElections().add(this);
		this.electionNode = electionNode;
		electionNode.getVoters().add(this);
	}
}
